package com.wao.menu.service;

import com.wao.menu.domain.Item;
import com.wao.menu.repository.ItemRepository;
import com.wao.menu.service.dto.ItemDTO;
import com.wao.menu.service.dto.ItemTreeDTO;
import com.wao.menu.service.mapper.ItemMapper;
import com.wao.menu.service.mapper.ItemTreeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Service Implementation for managing Item.
 */
@Service
@Transactional
public class ItemService {

    private final Logger log = LoggerFactory.getLogger(ItemService.class);

    private final ItemRepository itemRepository;

    private final ItemMapper itemMapper;

    private final ItemTreeMapper itemTreeMapper;

    public ItemService(ItemRepository itemRepository, ItemMapper itemMapper, ItemTreeMapper itemTreeMapper) {
        this.itemRepository = itemRepository;
        this.itemMapper = itemMapper;
        this.itemTreeMapper = itemTreeMapper;
    }

    /**
     * Save a item.
     *
     * @param itemDTO the entity to save
     * @return the persisted entity
     */
    public ItemDTO save(ItemDTO itemDTO) {
        log.debug("Request to save Item : {}", itemDTO);
        Item item = itemMapper.toEntity(itemDTO);
        item = itemRepository.save(item);
        return itemMapper.toDto(item);
    }

    /**
     * Get all the items.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<ItemDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Items");
        return itemRepository.findAll(pageable)
            .map(itemMapper::toDto);
    }

    /**
     * Get one item by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public ItemDTO findOne(Long id) {
        log.debug("Request to get Item : {}", id);
        Item item = itemRepository.findOne(id);
        return itemMapper.toDto(item);
    }

    /**
     * Delete the  item by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Item : {}", id);
        itemRepository.delete(id);
    }

    /**
     * Get all the items.
     *
     * @param menuId the id of menu
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<ItemTreeDTO> findTreeByMenu(Long menuId, Integer level) {
        log.debug("Request to get Item Tree by Menu : {}", menuId);
        if (level == null || level <= 0)
            level = 1;
        List<Item> sources = itemRepository.findByMenuIdAndLevelOrderByDisplayOrder(menuId, level);
        return itemTreeMapper.toDto(sources);
    }

    /**
     * Get all the items.
     *
     * @param menuId the id of menu
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<ItemDTO> findByMenuId(Long menuId, Pageable pageable) {
        log.debug("Request to get list by Menu : {}", menuId);
        return itemRepository.findByMenuId(menuId, pageable)
            .map(itemMapper::toDto);
    }

    /**
     * Get all the items.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<ItemDTO> findByParentId(Long parentId, Pageable pageable) {
        log.debug("Request to get all Categories");
        return itemRepository.findByParentId(parentId, pageable)
            .map(itemMapper::toDto);
    }
}
